package com.sdk.es.http.util;

import co.elastic.clients.elasticsearch._types.FieldValue;
import co.elastic.clients.elasticsearch._types.query_dsl.TermsQueryField;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.util.List;
import java.util.stream.Collectors;

public class TermsQueryFieldUtil {
    public static final JsonMapper JSON_MAPPER;

    public static final JacksonJsonpMapper JACKSON_JSONP_MAPPER;

    static {
        JSON_MAPPER = new JsonMapper();
        JACKSON_JSONP_MAPPER = new JacksonJsonpMapper(JSON_MAPPER);
    }

    public static <T> TermsQueryField of(List<T> values) {
        List<FieldValue> fieldValues = values.stream()
                .map(TermsQueryFieldUtil::of)
                .collect(Collectors.toList());
        return new TermsQueryField.Builder()
                .value(fieldValues)
                .build();
    }

    public static <T> FieldValue of(T value) {
        JsonData jsonData = JsonData.of(value, JACKSON_JSONP_MAPPER);
        return FieldValue.of(jsonData);
    }

}
